package com.novellotus.iov.carvice.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;


import com.novellotus.iov.carvice.R;

import static java.lang.Math.abs;
import static java.lang.Math.floor;

/**
 * Created by Rich on 2017/9/21.
 */

public class ScrollingImageView extends View {
    private final int speed;
    private Bitmap bitmap;

    private Rect clipBounds = new Rect();
    private int offset = 0;
    private int direction = 0;//0从左往右  1从右往左  2从上到下 3从下到上
    private boolean isStarted;

    public ScrollingImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.ParallaxView, 0, 0);
        try {
            speed = ta.getDimensionPixelSize(R.styleable.ParallaxView_speed, 10);
            direction = ta.getInteger(R.styleable.ParallaxView_direction, 0);
            bitmap = BitmapFactory.decodeResource(getResources(), ta.getResourceId(R.styleable.ParallaxView_src, 0));
        } finally {
            ta.recycle();
        }
        start();
    }

    public void setBitmap(Bitmap bitmap) {
        this.bitmap = bitmap;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), bitmap.getHeight());
    }

    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (canvas == null) {
            return;
        }
        //clipBounds获得画布大小
        canvas.getClipBounds(clipBounds);
        //偏移量
        int normalizedOffset = offset;
        //画布宽度
        int layerWidth = bitmap.getWidth();
        //画布长度
        int layerHeight = bitmap.getHeight();
        switch (direction) {
            //0从左往右  1从右往左  2从上到下 3从下到上
            case 0:
                if (offset > layerWidth) {
                    //方法floor给出的最大整数是小于或等于该参数。
                    offset -= (int) (floor(abs(normalizedOffset) / (float) layerWidth) * layerWidth);
                    //  offset -= layerWidth;
                }
                int right = offset;
                while (right < clipBounds.width()) {

                    canvas.drawBitmap(bitmap, -right, 0, null);
                    right += layerWidth;
                }
                break;
            case 1:
                if (offset < -layerWidth) {
                    //方法floor给出的最大整数是小于或等于该参数。
                    offset += (int) (floor(abs(normalizedOffset) / (float) layerWidth) * layerWidth);
                }
                int left = offset;
                while (left < clipBounds.width()) {
                    canvas.drawBitmap(bitmap, left, 0, null);
                    left += layerWidth;
                }
                break;
            case 2:
                if (offset < -layerHeight) {
                    //方法floor给出的最大整数是小于或等于该参数。
                    offset += (int) (floor(abs(normalizedOffset) / (float) layerHeight) * layerHeight);
                }
                int down = offset;
                while (down < clipBounds.height()) {
                    canvas.drawBitmap(bitmap, 0, -down, null);
                    down += layerHeight;
                }
                break;
            case 3:
                if (offset < -layerHeight) {
                    //方法floor给出的最大整数是小于或等于该参数。
                    offset += (int) (floor(abs(normalizedOffset) / (float) layerHeight) * layerHeight);
                    //   Log.e("Rich", "height:" + offset);
                }
                int up = offset;
                while (up < clipBounds.height()) {
                    canvas.drawBitmap(bitmap, 0, up, null);
                    up += layerHeight;
                    //   Log.e("Rich", "down:" + up);
                }
                break;
        }


        if (isStarted) {
            offset -= speed;
            postInvalidateOnAnimation();
        }
    }

    private float getBitmapLeft(int layerWidth, int left) {
        float bitmapLeft = left;
        if (speed < 0) {
            bitmapLeft = clipBounds.width() - layerWidth - left;
        }
        return bitmapLeft;
    }

    /**
     * Start the animation
     */
    public void start() {
        if (!isStarted) {
            isStarted = true;
            postInvalidateOnAnimation();
        }
    }

    /**
     * Stop the animation
     */
    public void stop() {
        if (isStarted) {
            isStarted = false;
            invalidate();
        }
    }
}
